{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Checking the validity of an API key:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "\n",
    "API_KEY = \"YOUR_API_KEY\"\n",
    "\n",
    "\n",
    "def initiate_client(api_key: str) -> openai.OpenAI:\n",
    "    print(\"Checking validity of API key\")\n",
    "    try:\n",
    "        openai.api_key = api_key\n",
    "        openai.models.list()\n",
    "    except openai.AuthenticationError as _e:\n",
    "        raise ValueError(\"Invalid API key.\")\n",
    "\n",
    "    print(\"Initiating client\")\n",
    "    return openai.OpenAI(api_key=api_key)\n",
    "\n",
    "\n",
    "initiate_client(API_KEY)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Adding value constraints to the `calculate_average` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "from typing import Iterable\n",
    "\n",
    "\n",
    "def calculate_average(numbers: Iterable[int | float]) -> float:\n",
    "    if not len(numbers):\n",
    "        return 0\n",
    "    return sum(numbers) / len(numbers)\n",
    "\n",
    "\n",
    "print(calculate_average([1, 2, 3, 4, 5]))  # 3.0\n",
    "print(calculate_average([]))  # 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Alternatively, you can raise an exception in case of an empty list (the better option IMO):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Empty list",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[5], line 6\u001b[0m\n\u001b[1;32m      3\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEmpty list\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m      4\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28msum\u001b[39m(numbers) \u001b[38;5;241m/\u001b[39m \u001b[38;5;28mlen\u001b[39m(numbers)\n\u001b[0;32m----> 6\u001b[0m \u001b[43mcalculate_average\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[0;32mIn[5], line 3\u001b[0m, in \u001b[0;36mcalculate_average\u001b[0;34m(numbers)\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcalculate_average\u001b[39m(numbers: Iterable[\u001b[38;5;28mint\u001b[39m \u001b[38;5;241m|\u001b[39m \u001b[38;5;28mfloat\u001b[39m]) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28mfloat\u001b[39m:\n\u001b[1;32m      2\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(numbers):\n\u001b[0;32m----> 3\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEmpty list\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m      4\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28msum\u001b[39m(numbers) \u001b[38;5;241m/\u001b[39m \u001b[38;5;28mlen\u001b[39m(numbers)\n",
      "\u001b[0;31mValueError\u001b[0m: Empty list"
     ]
    }
   ],
   "source": [
    "def calculate_average(numbers: Iterable[int | float]) -> float:\n",
    "    if not len(numbers):\n",
    "        raise ValueError(\"Empty list\")\n",
    "    return sum(numbers) / len(numbers)\n",
    "\n",
    "\n",
    "calculate_average([])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
